package com.cakeclient.clientsoap.contract;


import com.cakeclient.Model.CatalogCategory;
import org.ksoap2.serialization.NullSoapObject;
import org.ksoap2.serialization.SoapObject;

public class ProductCategory extends ContractObject {
    public int id;
    public String name;
    public String illustration;
    public String description;
    public SoapArray<Integer> childProductCategoryId;
    public SoapArray<Integer> childProductTypeId;

    public ProductCategory(CatalogCategory actualCategory) {
        if (actualCategory.getRemoteID() != null)
            id = Integer.valueOf(actualCategory.getRemoteID().toString());
        name = actualCategory.getName();
        illustration = actualCategory.getIllustration();
        description = actualCategory.getDescription();
        childProductCategoryId = new SoapArray<Integer>(actualCategory.getChildCategoriesId().size(), "childProductCategoryId");
        for (Long id : actualCategory.getChildCategoriesId()) {
            childProductCategoryId.add(Integer.valueOf(id.toString()));
        }
        childProductTypeId = new SoapArray<Integer>(actualCategory.getChildProductTypeId().size(), "childProductTypeId");
        for (Long id : actualCategory.getChildProductTypeId()) {
            childProductTypeId.add(Integer.valueOf(id.toString()));
        }
    }

    public ProductCategory() {

    }

    public void fill(SoapObject soapObject) {
        this.id = Integer.parseInt(soapObject.getPropertyAsString("id"));
        this.name = soapObject.getPropertySafelyAsString("name").trim();
        this.illustration = soapObject.getPropertySafelyAsString("illustration");
        this.description = soapObject.getPropertySafelyAsString("description");

        SoapObject s = soapObject.getPropertySafely("childProductCategoryId") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("childProductCategoryId");
        if (s != null) {
            childProductCategoryId = new SoapArray<Integer>(s.getPropertyCount(), "childProductCategoryId");
            for (int i = 0; i < s.getPropertyCount(); i++) {
                childProductCategoryId.add(Integer.parseInt(s.getProperty(i).toString()));
            }
        }

        SoapObject localPricesSoap = soapObject.getPropertySafely("childProductTypeId") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("childProductTypeId");
        if (localPricesSoap != null) {
            childProductTypeId = new SoapArray<Integer>(localPricesSoap.getPropertyCount(), "childProductTypeId");
            for (int i = 0; i < localPricesSoap.getPropertyCount(); i++) {
                childProductTypeId.add(Integer.parseInt(localPricesSoap.getProperty(i).toString()));
            }
        }
    }
}
